package com.llxwz.controller;


import com.llxwz.entity.Comment;
import com.llxwz.entity.DiscussPost;
import com.llxwz.entity.Page;
import com.llxwz.entity.User;
import com.llxwz.service.CommentService;
import com.llxwz.service.DiscussPostService;
import com.llxwz.service.LikeService;
import com.llxwz.service.UserService;
import com.llxwz.util.CommunityConstant;
import com.llxwz.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProfileController implements CommunityConstant {

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @GetMapping("/whoPost/{userId}")
    public String whoPost(@PathVariable("userId")int userId,Model model, Page page){
        User user = userService.findById(userId);

        int postRows = discussPostService.findDiscussPostRows(user.getId());
        model.addAttribute("postRows",postRows);
        // 分页信息
        page.setLimit(5);
        page.setPath("/whoPost/"+userId);
        page.setRows(postRows);
        model.addAttribute("user",user);

        List<DiscussPost> discussPosts = discussPostService.findDiscussPosts(user.getId(),page.getOffset(),page.getLimit());
        List<Map<String,Object>> result = new ArrayList<>();
        if(discussPosts != null){
            for(DiscussPost discussPost:discussPosts){
                Map<String,Object> map = new HashMap<>();
                map.put("post",discussPost);
                map.put("user",user);

                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST,discussPost.getId());
                map.put("likeCount",likeCount);

                result.add(map);
            }
        }

        model.addAttribute("result",result);

        return "site/my-post";
    }


    @GetMapping("/whoReply/{userId}")
    public String whoReply(@PathVariable("userId")int userId,Model model,Page page){

        User user = userService.findById(userId);

        int replyRows = commentService.findCountByUserId(ENTITY_TYPE_POST,user.getId());
        model.addAttribute("replyRows",replyRows);
        // 分页信息
        page.setLimit(10);
        page.setPath("/whoReply/"+userId);
        page.setRows(replyRows);

        model.addAttribute("user",user);

        List<Comment> replys = commentService.findCommentsByUserId(ENTITY_TYPE_POST,user.getId(),page.getOffset(),page.getLimit());
        List<Map<String,Object>> result = new ArrayList<>();
        if(replys != null){
            for(Comment reply:replys){
                Map<String,Object> map = new HashMap<>();
                map.put("reply",reply);
                map.put("user",user);
                map.put("post",discussPostService.findDiscussPostById(reply.getEntityId()));

                result.add(map);
            }
        }

        model.addAttribute("result",result);

        return "site/my-reply";
    }

}
